package com.xique.door.service.impl;


import com.xique.common.core.text.Convert;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.MathUtil;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.bean.domain.User;
import com.xique.door.bean.domain.Visitor;
import com.xique.door.bean.request.visitor.VisitorListRequest;
import com.xique.door.command.VisitorCommandService;
import com.xique.door.mapper.UserMapper;
import com.xique.door.mapper.VisitorMapper;
import com.xique.door.service.IVisitorService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 访客信息Service业务层处理
 *
 * @author caogq
 * @date 2021-06-12
 */
@Service
public class VisitorServiceImpl implements IVisitorService
{
    @Autowired
    private VisitorMapper visitorMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private VisitorCommandService visitorCommandService;

    /**
     * 查询访客信息
     *
     * @param id 访客信息ID
     * @return 访客信息
     */
    @Override
    public Visitor selectVisitorById(Long id)
    {
        Visitor visitor = visitorMapper.selectVisitorById(id);
        User user = userMapper.selectUserById(visitor.getUserId());
        visitor.setUserName(user.getUserName());
        return visitor;
    }

    @Override
    public List<Visitor> selectVisitorByIds(List<Long> ids) {
        if (ids.isEmpty()) {
            return new ArrayList<>();
        }
        return visitorMapper.selectVisitorByIds(ids);
    }

    /**
     * 查询访客信息列表
     *
     * @param visitor 访客信息
     * @return 访客信息
     */
    @Override
    public List<Visitor> selectVisitorList(Visitor visitor)
    {
        return visitorMapper.selectVisitorList(visitor);
    }

    @Override
    public List<Visitor> selectVisitorListV2(VisitorListRequest request) {
        return visitorMapper.selectVisitorListV2(request);
    }

    @Override
    public List<Visitor> selectVisitorListByCustomIds(List<String> customIds) {
        if (CollectionUtils.isEmpty(customIds)) {
            return new ArrayList<>();
        }
        return visitorMapper.selectVisitorListByCustomIds(customIds);
    }

    /**
     * 新增访客信息
     *
     * @param visitor 访客信息
     * @return 结果
     */
    @Override
    public int insertVisitor(Visitor visitor)
    {
        //32位id生成逻辑 = 13位时间戳+19位随机数
        String customId  = System.currentTimeMillis()+ MathUtil.getRandom(19);
        visitor.setCustomId(customId);
        visitor.setCreateTime(DateUtils.getNowDate());
        visitor.setCreateUser(SecurityUtils.getUserId());
        visitor.setUpdateUser(SecurityUtils.getUserId());
        visitor.setUpdateTime(DateUtils.getNowDate());
        return visitorMapper.insertVisitor(visitor);
    }

    /**
     * 修改访客信息
     *
     * @param visitor 访客信息
     * @return 结果
     */
    @Override
    public int updateVisitor(Visitor visitor)
    {
        visitor.setUpdateTime(DateUtils.getNowDate());
        visitor.setUpdateUser(SecurityUtils.getUserId());
        return visitorMapper.updateVisitor(visitor);
    }

    /**
     * 删除访客信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteVisitorByIds(String ids)
    {
        String[] delIds = Convert.toStrArray(ids);
        // 下发状态
        AjaxResult result = visitorCommandService.batchDelVisitor(ids);
        if ( result.get("code").equals(AjaxResult.Type.ERROR) ){
            return result;
        }
        int i = visitorMapper.deleteVisitorByIds(delIds);
        if (i< 0 ){
            return AjaxResult.error("删除失败");
        }

        return  AjaxResult.success("删除成功");
    }

    /**
     * 删除访客信息信息
     *
     * @param id 访客信息ID
     * @return 结果
     */
    @Override
    public int deleteVisitorById(Long id)
    {
        return visitorMapper.deleteVisitorById(id);
    }

    @Override
    public List<Visitor> selectUnAssignVisitorlist(Visitor visitor) {
        return  visitorMapper.selectUnAssignVisitorlist(visitor);
    }

    /**
     * @param userId
     * @param visitorIds
     * @return
     */
    @Override
    public int insertUserVisitor(Long userId, String visitorIds) {
        List<String> ids = Arrays.asList(visitorIds.split(","));
        return visitorMapper.batchUpdate(userId,ids);
    }

    @Override
    public List<Visitor> selectUserVisitorList(Map<String, Object> map) {
        return   visitorMapper.selectUserVisitorList(map);

    }

    @Override
    public int clearVisitorUrl(Long visitorId) {
        return visitorMapper.clearVisitorUrl(visitorId);
    }
}
